package greentanning;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;

import utilities.Util;
import constants.ERsBank;
import constants.ERsDirection;
import constants.ERsFontType;
import constants.ERsInteract;
import constants.ERsInterfaceOption;
import constants.ERsItem;
import constants.ERsNPC;
 
@ScriptManifest(name = "GreenTanning", author = "Josephex", version = 1.0D, info = "Tans Green Dragon hides. Must have enough Coin items in the inventory and the bank clean, only with a few items including green hides, for working flawless.")
public class GreenTanning extends Script {
    
    private String msgToIdentifySuccessLoop = "tanner tans";
    
    private String imageNameToLoad;
    
    private final String NAME_ITEM_TO_PROCUCE = "Green dragonhides Tanned => ";
    private final String NAME_ITEM_TO_PROCUCE_PERHOUR = "Green dragonhides Tanned / HR => ";
    
    private final int myGreenDHInterfaceParentID = 324;
	private final int myGreenDHInterfaceChildID = 104;
	private State state;
	private Util util;
	
	public enum State{
		  OPENING_BANK
		, IS_MOVING_ON_WAY
		, GO_ELLIS
		, GO_BANK
		, DEPOSITING_ALL_IN_BANK
		, WITHDRAWING_ITEMS_TO_INVENTORY
		, LOGING_OUT
		, INTERACTING_WITH_ELLIS
		, IDLE
	}
 
    public void onStartScript(String msgToIdentifySuccessLoop) {
    	this.msgToIdentifySuccessLoop = msgToIdentifySuccessLoop;
    	this.onStart();
    }
    
    public void onStartScript(String msgToIdentifySuccessLoop, String imageNameToLoad) {
    	this.msgToIdentifySuccessLoop 	= msgToIdentifySuccessLoop;
    	this.imageNameToLoad			= imageNameToLoad;
    	this.onStart();
    }
    
    public void onStart() {
    	this.util = new Util(this);
    	util.cargarImagen(this.imageNameToLoad);
    }
 
    public int onLoop() throws InterruptedException {
    	state = getState();
    	
		switch (state){
			case IDLE:{
				util.idle();
				break;
			}
			case IS_MOVING_ON_WAY:{
				util.run();
				util.isPlayerMoving();
				break;
			}
			case GO_ELLIS:{
				util.run();
	    		util.walkTo(ERsNPC.Ellis,ERsDirection.RIGHT);
				break;
			}
			case WITHDRAWING_ITEMS_TO_INVENTORY:{
				util.withdrawAllInInventory(ERsItem.Green_dragonhide);
				util.addItemsProducedCheckingLogMessage(this.msgToIdentifySuccessLoop);
				break;
			}
			case DEPOSITING_ALL_IN_BANK:{
				util.depositAllInventoryInBankExcept(ERsItem.Cash);
				break;
			}
			case INTERACTING_WITH_ELLIS:{
				util.interactWith(ERsNPC.Ellis, ERsInteract.Trade);
				util.clickOnInterface(this.myGreenDHInterfaceParentID, this.myGreenDHInterfaceChildID, ERsInterfaceOption.Tan_All);
				break;
			}
			case LOGING_OUT:{
				util.logout();
				break;
			}
			case OPENING_BANK:{
				util.openBank();
				break;
			}
			case GO_BANK:{
				util.run();
	    		util.walkTo(ERsBank.Bank_booth);
				break;
			}
		}
		
        return random(100);
    }
    
    public State getState() throws InterruptedException{
    	
    	if(util.isPlayerMoving()){
			return State.IS_MOVING_ON_WAY;
		}else{
	    	if(util.playerIsIn(ERsBank.Bank_booth)){
	    		if(util.inventoryContains(ERsItem.Green_dragonhide)){
	    			return State.GO_ELLIS;
	    		}else{
	    			if(util.getBank().isOpen()){
	    				if(util.isInventoryEmptyExcept(ERsItem.Cash)){
	    					if(util.bankContains(ERsItem.Green_dragonhide)){
	    						return State.WITHDRAWING_ITEMS_TO_INVENTORY;
	    					}else{
//	            				return State.LOGING_OUT;    						
	    						return State.IDLE;
	    					}
	    				}else{
	        				return State.DEPOSITING_ALL_IN_BANK;
	    				}
	    			}else{
	        			return State.OPENING_BANK;	
	    			}
	    		}
	    	}else{
	    		if(util.playerIsIn(ERsNPC.Ellis)){
	    			if(util.inventoryContains(ERsItem.Green_dragonhide)){
	    				return State.INTERACTING_WITH_ELLIS;
	    			}else{
	    				if(util.inventoryContains(ERsItem.Green_d_leather)){
	    					return State.GO_BANK;
	    				}else{
	    					return State.OPENING_BANK;
	    				}
	    			}
	    		}else{
	    			if(util.inventoryContains(ERsItem.Green_dragonhide)){
	    				return State.GO_ELLIS;
	    			}else{
	    				return State.GO_BANK;
	    			}
	    		}
	    	}
		}
    }
    
    public void onMessage(String message) {
//    	util.addItemsProducedCheckingLogMessage(this.msgToIdentifySuccessLoop, this.quantityItemsWaitedPerCycle);
    }
 
    public void onPaint(Graphics g) {
    	Graphics2D screen = (Graphics2D)g;
    	
    	util.setFontSettingForScreen(screen,Color.BLACK, ERsFontType.Arial, Font.PLAIN, 18);
    	if(state != null){
        	screen.drawString("State => "+ state.toString(), 170, 365 );
    	}
    	screen.drawString(NAME_ITEM_TO_PROCUCE 			+ util.getItemsProduced(), 170, 395 );
    	screen.drawString(NAME_ITEM_TO_PROCUCE_PERHOUR 	+ util.getItemsProducedPerHour(), 170, 425 );
    	screen.drawString("Runtime => "+ util.getTimeScriptPerforming(), 170, 455);
    }
    
}
